home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Programming / vahunz / source / ubiqx / ubi_BinTree.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-17  |  6.1 KB  |  391 lines

  1. /*
  2.  * This source file is part of Vahunz,
  3.  * a tool to make source code un-/more legible.
  4.  *
  5.  *--------------------------------------------------------------------------
  6.  *
  7.  * Vahunz and the Ugly library are Copyright (C) 1998 by
  8.  * Thomas Aglassinger <agi@giga.or.at>
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Refer to the manual for more information.
  13.  *
  14.  *--------------------------------------------------------------------------
  15.  *
  16.  * Ubiqx library is Copyright (C) 1991-1998 by
  17.  * Christopher R. Hertel <crh@ubiqx.mn.org>
  18.  *
  19.  * Ubiqx library is free software; you can redistribute it and/or
  20.  * modify it under the terms of the GNU Library General Public
  21.  * License as published by the Free Software Foundation; either
  22.  * version 2 of the License, or (at your option) any later version.
  23.  *
  24.  */
  25. #include "ubi_BinTree.h" 
  26. #include <stdlib.h> 
  27. static char f2A[] = "ubi_BinTree\n\
  28. \t$Revision: 2.5 $\n\
  29. \t$Date: 1997/12/23 03:56:29 $\n\
  30. \t$Author: crh $\n";
  31. static l1Z l4I( w7Nt m3Sk,
  32. f7P m9X,
  33. register l1Z p )
  34. {
  35. long e0Y;
  36. while( p && (( e0Y = f2V((*m3Sk)(m9X, p)) ) != t3Db) )
  37. p = p->b9H[e0Y];
  38. return( p );
  39. static l1Z p7Q( f7P j3I,
  40. l1Z p,
  41. l1Z *t6Z,
  42. char *k5O,
  43. w7Nt q6X )
  44. {
  45. register l1Z t6V = p;
  46. l1Z x2Ez = NULL;
  47. int h1J = t3Db;
  48. int t1W;
  49. while( t6V
  50. && (t3Db != (t1W = f2V((*q6X)(j3I, t6V)))) )
  51. {
  52. x2Ez = t6V; 
  53. h1J = t1W; 
  54. t6V = t6V->b9H[t1W]; 
  55. }
  56. *t6Z = x2Ez; 
  57. *k5O = h1J;
  58. return( t6V );
  59. static void l9H( l1Z *e2L,
  60. l1Z k7H,
  61. l1Z t2M )
  62. {
  63. register int i;
  64. register int g4D = sizeof( c9M );
  65. for( i = 0; i < g4D; i++ ) 
  66. ((unsigned char *)t2M)[i] = ((unsigned char *)k7H)[i];
  67. (*e2L) = t2M; 
  68. if( k7H->b9H[z7V] )
  69. (k7H->b9H[z7V])->b9H[z6Y] = t2M;
  70. if( k7H->b9H[i7C] )
  71. (k7H->b9H[i7C])->b9H[z6Y] = t2M;
  72. static void q7H( o4R x8O,
  73. l1Z v6L,
  74. l1Z h3D )
  75. {
  76. l1Z *Parent;
  77. c9M u4M;
  78. l1Z v3O = &u4M;
  79. if( v6L->b9H[z6Y] )
  80. Parent = &((v6L->b9H[z6Y])->b9H[(int)(v6L->k5O)]);
  81. else
  82. Parent = &(x8O->k6P);
  83. l9H( Parent, v6L, v3O );
  84. if( h3D->b9H[z6Y] )
  85. Parent = &((h3D->b9H[z6Y])->b9H[(int)(h3D->k5O)]);
  86. else
  87. Parent = &(x8O->k6P);
  88. l9H( Parent, h3D, v6L );
  89. if( v3O->b9H[z6Y] )
  90. Parent = &((v3O->b9H[z6Y])->b9H[(int)(v3O->k5O)]);
  91. else
  92. Parent = &(x8O->k6P);
  93. l9H( Parent, v3O, h3D );
  94. static l1Z b7K( register l1Z P,
  95. register int i8L )
  96. {
  97. l1Z Q = NULL;
  98. while( P )
  99. {
  100. Q = P;
  101. P = P->b9H[ i8L ];
  102. }
  103. return( Q );
  104. static l1Z u9X( register l1Z P,
  105. register int i8L )
  106. {
  107. if( P )
  108. {
  109. if( P->b9H[ i8L ] )
  110. return( b7K( P->b9H[ i8L ], (char)y2Bn(i8L) ) );
  111. else
  112. while( P->b9H[ z6Y ] )
  113. {
  114. if( (P->b9H[ z6Y ])->b9H[ i8L ] == P )
  115. P = P->b9H[ z6Y ];
  116. else
  117. return( P->b9H[ z6Y ] );
  118. }
  119. }
  120. return( NULL );
  121. static l1Z Border( o4R x8O,
  122. f7P m9X,
  123. l1Z p,
  124. int i8L )
  125. {
  126. register l1Z q;
  127. if( !p3K( x8O ) || (z6Y == i8L) )
  128. return( p );
  129. q = p->b9H[z6Y];
  130. while( q && (t3Db == f2V( (*(x8O->m3Sk))(m9X, q) )) )
  131. {
  132. p = q;
  133. q = p->b9H[z6Y];
  134. }
  135. q = p->b9H[i8L];
  136. while( q )
  137. {
  138. q = l4I( x8O->m3Sk, m9X, q );
  139. if( q )
  140. {
  141. p = q;
  142. q = p->b9H[i8L];
  143. }
  144. }
  145. return( p );
  146. long t2S( register long x )
  147. {
  148. return( (x)?((x>0)?(1):(-1)):(0) );
  149. l1Z u3Ny( l1Z y2B )
  150. {
  151. y2B->b9H[ z7V ] = NULL;
  152. y2B->b9H[ z6Y ] = NULL;
  153. y2B->b9H[ i7C ] = NULL;
  154. y2B->k5O = t3Db;
  155. return( y2B );
  156. o4R q4C( o4R x8O,
  157. w7Nt b2W,
  158. unsigned char Flags )
  159. {
  160. if( x8O )
  161. {
  162. x8O->k6P = NULL;
  163. x8O->count = 0L;
  164. x8O->m3Sk = b2W;
  165. x8O->flags = (Flags & r9O) ? r9O : Flags;
  166. return( x8O );
  167. f6W k2M( o4R x8O,
  168. l1Z j4E,
  169. f7P j7S,
  170. l1Z *s3L )
  171. {
  172. l1Z r9V,
  173. e2L = NULL;
  174. char e0Y;
  175. if( !(s3L) ) 
  176. s3L = &r9V;
  177. (void)u3Ny( j4E ); 
  178. *s3L = p7Q(j7S, (x8O->k6P), &e2L, &e0Y, (x8O->m3Sk));
  179. if (!(*s3L)) 
  180. {
  181. if (!(e2L))
  182. x8O->k6P = j4E;
  183. else
  184. {
  185. e2L->b9H[(int)e0Y] = j4E;
  186. j4E->b9H[z6Y] = e2L;
  187. j4E->k5O = e0Y;
  188. }
  189. (x8O->count)++;
  190. return( s0E );
  191. }
  192. if( p3K(x8O) ) 
  193. {
  194. l1Z q;
  195. e0Y = i7C;
  196. q = (*s3L);
  197. *s3L = NULL;
  198. while( q )
  199. {
  200. e2L = q;
  201. if( e0Y == t3Db )
  202. e0Y = i7C;
  203. q = q->b9H[(int)e0Y];
  204. if ( q )
  205. e0Y = f2V( (*(x8O->m3Sk))(j7S, q) );
  206. }
  207. e2L->b9H[(int)e0Y] = j4E;
  208. j4E->b9H[z6Y] = e2L;
  209. j4E->k5O = e0Y;
  210. (x8O->count)++;
  211. return( s0E );
  212. }
  213. if( b4Y(x8O) ) 
  214. {
  215. if (!(e2L))
  216. l9H( &(x8O->k6P), *s3L, j4E );
  217. else
  218. l9H( &(e2L->b9H[(int)((*s3L)->k5O)]),
  219. *s3L, j4E );
  220. return( s0E );
  221. }
  222. return( b9P ); 
  223. l1Z v6T( o4R x8O,
  224. l1Z b2L )
  225. {
  226. l1Z p,
  227. *t6Z;
  228. int e0Y;
  229. if( (b2L->b9H[z7V]) && (b2L->b9H[i7C]) )
  230. q7H( x8O, b2L, l7C( b2L ) );
  231. if (b2L->b9H[z6Y])
  232. t6Z = &((b2L->b9H[z6Y])->b9H[(int)(b2L->k5O)]);
  233. else
  234. t6Z = &( x8O->k6P );
  235. e0Y = ((b2L->b9H[z7V])?z7V:i7C);
  236. p = (b2L->b9H[e0Y]);
  237. if( p )
  238. {
  239. p->b9H[z6Y] = b2L->b9H[z6Y];
  240. p->k5O = b2L->k5O;
  241. }
  242. (*t6Z) = p;
  243. (x8O->count)--;
  244. return( b2L );
  245. l1Z p2P( o4R x8O,
  246. f7P m9X,
  247. w3K w5Y )
  248. {
  249. register l1Z p;
  250. l1Z e2L;
  251. char p4Ny;
  252. p = p7Q( m9X,
  253. x8O->k6P,
  254. &e2L,
  255. &p4Ny,
  256. x8O->m3Sk );
  257. if( p ) 
  258. {
  259. switch( w5Y )
  260. {
  261. case g0I: 
  262. p = Border( x8O, m9X, p, z7V );
  263. return( u9X( p, z7V ) );
  264. case e6B: 
  265. p = Border( x8O, m9X, p, i7C );
  266. return( u9X( p, i7C ) );
  267. default:
  268. p = Border( x8O, m9X, p, z7V );
  269. return( p );
  270. }
  271. }
  272. if( j2M == w5Y ) 
  273. return( NULL ); 
  274. if( (g0I == w5Y) || (u4I == w5Y) )
  275. return( (z7V == p4Ny) ? u9X( e2L, p4Ny ) : e2L );
  276. else
  277. return( (i7C == p4Ny) ? u9X( e2L, p4Ny ) : e2L );
  278. l1Z f9B( o4R x8O,
  279. f7P m9X )
  280. {
  281. return( l4I( x8O->m3Sk, m9X, x8O->k6P ) );
  282. l1Z l1X( l1Z P )
  283. {
  284. return( u9X( P, i7C ) );
  285. l1Z l7C( l1Z P )
  286. {
  287. return( u9X( P, z7V ) );
  288. l1Z o7R( l1Z P )
  289. {
  290. return( b7K( P, z7V ) );
  291. l1Z d4K( l1Z P )
  292. {
  293. return( b7K( P, i7C ) );
  294. l1Z g1V( o4R x8O,
  295. f7P n8U,
  296. l1Z p )
  297. {
  298. if( !p || f2V( (*(x8O->m3Sk))( n8U, p ) != t3Db ) )
  299. return( NULL );
  300. return( Border( x8O, n8U, p, z7V ) );
  301. l1Z z5H( o4R x8O,
  302. f7P n8U,
  303. l1Z p )
  304. {
  305. if( !p || f2V( (*(x8O->m3Sk))( n8U, p ) != t3Db ) )
  306. return( NULL );
  307. return( Border( x8O, n8U, p, i7C ) );
  308. f6W b6S( o4R x8O,
  309. c7W d2L,
  310. void *UserData )
  311. {
  312. l1Z p;
  313. if( !(p = o7R( x8O->k6P )) ) return( b9P );
  314. while( p )
  315. {
  316. (*d2L)( p, UserData );
  317. p = l1X( p );
  318. }
  319. return( s0E );
  320. f6W q1C( o4R x8O,
  321. r2L q1P )
  322. {
  323. l1Z p, q;
  324. if( !(x8O) || !(q1P) )
  325. return( b9P );
  326. p = o7R( x8O->k6P );
  327. while( p )
  328. {
  329. q = p;
  330. while( q->b9H[i7C] )
  331. q = b7K( q->b9H[i7C], z7V );
  332. p = q->b9H[z6Y];
  333. if( p )
  334. p->b9H[ ((p->b9H[z7V] == q)?z7V:i7C) ] = NULL;
  335. (*q1P)((void *)q);
  336. }
  337. (void)q4C( x8O,
  338. x8O->m3Sk,
  339. x8O->flags );
  340. return( s0E );
  341. l1Z s6Y( l1Z f5Y )
  342. {
  343. l1Z x1M = NULL;
  344. int i8L = z7V;
  345. while( NULL != f5Y )
  346. {
  347. x1M = f5Y;
  348. f5Y = x1M->b9H[ i8L ];
  349. if( NULL == f5Y )
  350. {
  351. i8L = y2Bn( i8L );
  352. f5Y = x1M->b9H[ i8L ];
  353. }
  354. }
  355. return( x1M );
  356. int x0K( int c8H, char *list[] )
  357. {
  358. if( c8H > 0 )
  359. {
  360. list[0] = f2A;
  361. if( c8H > 1 )
  362. list[1] = NULL;
  363. return( 1 );
  364. }
  365. return( 0 );
  366.